package io.renren.modules.oss.cloud;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import io.renren.common.exception.BizCodeEnum;
import io.renren.common.exception.RRException;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.Map;

/**
 * @author: xiaomi
 * @date: 2022/2/15
 * @description:
 */
public class COSCloudStorageService extends CloudStorageService {
    private COSClient client;

    public COSCloudStorageService(CloudStorageConfig config) {
        this.config = config;

        //初始化
        init();
    }

    private void init() {
        COSCredentials cred = new BasicCOSCredentials(config.getQcloudSecretId(), config.getQcloudSecretKey());
// 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region(config.getQcloudRegion());
        ClientConfig clientConfig = new ClientConfig(region);
// 这里建议设置使用 https 协议
// 从 5.6.54 版本开始，默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);

        client = new COSClient(cred, clientConfig);
    }

    @Override
    public String upload(String localPath, String key) {
        File file = new File(localPath);
        if (!file.exists()) {
            throw new RRException(BizCodeEnum.FILE_NOT_FOUND.getMsg());
        }
        String finalKey = config.getQcloudPrefix() + key;
        PutObjectRequest putObjectRequest = new PutObjectRequest(config.getQcloudBucketName(), finalKey, file);
        PutObjectResult putObjectResult = client.putObject(putObjectRequest);
        return finalKey;
    }

    /**
     * 生成预签名地址。
     * 有三重防护：
     * 1. URL 具有超时时间；
     * 2. 请求头审核
     * 3. 请求参数审核
     *
     * @param key
     * @param minutes
     * @param headers
     * @param params
     * @return
     */
    @Override
    public String getPresignedURL(String key, int minutes, Map<String, String> headers, Map<String, String> params) {
        Date expirationDate = new Date(System.currentTimeMillis() + minutes * 60 * 1000);
        URL url = client.generatePresignedUrl(config.getQcloudBucketName(), key, expirationDate, HttpMethodName.GET, headers, params);
        return url.toString();
    }

    @Override
    public String upload(byte[] data, String path) {
        return null;
    }

    @Override
    public String uploadSuffix(byte[] data, String suffix) {
        return null;
    }

    @Override
    public String upload(InputStream inputStream, String path) {
        return null;
    }

    @Override
    public String uploadSuffix(InputStream inputStream, String suffix) {
        return null;
    }
}
